package com.library.bookmanagement.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.library.bookmanagement.entity.Admin;
import com.library.bookmanagement.entity.SuperAdmin;
import com.library.bookmanagement.mapper.AdminMapper;
import com.library.bookmanagement.mapper.SuperAdminMapper;
import com.library.bookmanagement.service.SuperAdminService;
import com.library.bookmanagement.util.Constant;
import com.library.bookmanagement.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：ZXM
 * @date ：Created in 2020/5/9 9:11
 * @description：超级管理员业务逻辑层接口
 * */
@Service
public class SuperAdminServiceImpl  implements SuperAdminService {
    @Autowired
    private SuperAdminMapper superAdminMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
    * @Description: 显示所有管理员列表
    * @Param: [pageNum, pageSize]
    * @return: com.library.bookmanagement.util.ResultUtil<java.util.Map<java.lang.String,java.lang.Object>>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil<Map<String,Object>> QueryAdminAll(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Admin> admins = adminMapper.selectAdminAll();
        if(admins != null){
            HashMap<String,Object> map = new HashMap<>();
            map.put(Constant.MESSAGE_TOTALNUM,admins.getTotal());
            map.put(Constant.MEASSAGE_DATAS,admins);
            return ResultUtil.ok().message("管理员查询成功").data(map);
        }
        return ResultUtil.error().message("管理员信息查询失败!");
    }

    /**
    * @Description: 添加管理员
    * @Param: [admin]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil addAdmin(Admin admin) {
        String email = admin.getaEmail();
        List<Admin> admins = adminMapper.selectByEmail(email);
        if(admins.size() == 0){
            adminMapper.insertSelective(admin);
            return ResultUtil.ok().message("管理员添加成功！");
        }else if(admins.size() != 0){
            return ResultUtil.error().message("该邮箱已被注册，请重新选择注册邮箱!");
        }else{
            return ResultUtil.error().message("管理员注册失败!");
        }
    }

    /**
    * @Description: 添加超级管理员
    * @Param: [superAdmin]
    * @return: void
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public void addSuperAdmin(SuperAdmin superAdmin) {
        superAdminMapper.insert(superAdmin);
    }

    /**
    * @Description: 删除管理员
    * @Param: [ids]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil deleteById(List<Integer> ids) {
        for (Integer id:ids){
            Boolean aBoolean = adminMapper.deleteByPrimaryKey(id);
            if(!aBoolean){
                return ResultUtil.error().message("管理员删除失败");
            }
        }
        return ResultUtil.ok().message("管理员删除成功");
    }

    /**
    * @Description: 返回已选择的用户信息
    * @Param: [adminId]
    * @return: com.library.bookmanagement.util.ResultUtil<com.library.bookmanagement.entity.Admin>
    * @Author: ZXM
    * @Date: 2020/6/19
    */
    @Override
    public ResultUtil<Admin> queryAdminById(Integer adminId) {
        Admin admin = adminMapper.selectByPrimaryKey(adminId);
        if(admin != null){
            return ResultUtil.ok().data(admin);
        }else{
            return ResultUtil.error().message("查询出的管理员不存在!");
        }
    }

    /**
    * @Description: 修改用户信息
    * @Param: [admin]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/19
    */
    @Override
    public ResultUtil editAdminInfo(Admin admin) {
        int i = adminMapper.updateByPrimaryKeySelective(admin);
        if(i != -1){
            return ResultUtil.ok().message("修改成功!");
        }else{
            return ResultUtil.error().message("修改失败!");
        }
    }

    /**
    * @Description: 重置管理员密码
    * @Param: [adminId]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/19
    */
    @Override
    public ResultUtil editAdminPassword(Integer adminId) {
        Admin admin = adminMapper.selectByPrimaryKey(adminId);
        String encode = bCryptPasswordEncoder.encode(Constant.EDIT_ADMIN_PASSWORD);
        admin.setPassword(encode);
        int i = adminMapper.updateByPrimaryKeySelective(admin);
        if(i != -1){
            return ResultUtil.ok().message("修改密码成功!");
        }else{
            return ResultUtil.error().message("修改密码失败!");
        }
    }
}
